include('shared.lua')

CreateConVar("cl_draw_catmullrom_cameras", "1")

ENT.RenderGroup = RENDERGROUP_BOTH
local NODE_MATERIAL = Material( "sprites/light_frame_grip" )

local TEMP = Material("sprites/physg_glow1")
local TRACK_MATERIAL  = Material("sprites/dacre_track")
local LINK_MAT = Material("cable/physbeam")
local SpriteOffset = Vector(0, 0, 32)

local TRACK_MATERIALB = Material("cable/white")
local GlowMat = Material("sprites/dacre_highlight")

local function randomNumber()
	local ran = math.random(10.0,20.0)
	ran = (ran - 15.0)/ 10.0
	return ran
end

function ENT:CreateParticle()

	self.particles = self.particles or {}
	
	local generaDirection = Vector(self:GetNWFloat("generation_dirx"),self:GetNWFloat("generation_diry"),self:GetNWFloat("generation_dirz"))
	local randomDirection = Vector(randomNumber(),randomNumber(),randomNumber()) * self:GetNWFloat("generation_dirvar")

	local generaDisplacement = Vector(randomNumber() * self:GetNWFloat("generation_disx"),randomNumber() * self:GetNWFloat("generation_disz"),randomNumber() * self:GetNWFloat("generation_disy"))

	local generaAcceleration = Vector(self:GetNWFloat("generation_acex"),self:GetNWFloat("generation_acey"),self:GetNWFloat("generation_acez"))
	local randomAcceleration = Vector(randomNumber(),randomNumber(),randomNumber()) * self:GetNWFloat("generation_acevar")

	local generaRadius = self:GetNWFloat("param_rad")
	local randomRadius = self:GetNWInt("param_radvar")

	local generaRotate = self:GetNWFloat("param_rotate")
	local randomRotate = self:GetNWInt("param_rotatevar")
	
	local generaLife = self:GetNWFloat("param_life")
	local randomLife = self:GetNWInt("param_lifevar")
	

	local particle = {}
	particle.position = Vector(0,0,0)
	particle.velocity = Vector(generaDirection.x + randomDirection.x, generaDirection.z + randomDirection.z, generaDirection.y + randomDirection.y)
	particle.acceleration = Vector(generaAcceleration.x + randomAcceleration.x, generaAcceleration.z + randomAcceleration.z, generaAcceleration.y + randomAcceleration.y)
	particle.initpos = generaDisplacement
	particle.radius = math.max(1.0, generaRadius + (randomNumber() * randomRadius))
	particle.lifetime = 0.0
	particle.lifelength = math.max(1.0, generaLife + ((0.5+randomNumber()) * randomLife))
	
	self.population = self.population + 1
	table.insert(self.particles, 1, particle)

end

function ENT:UpdateParticles()

	local dT = .1
	local idx = 1
	for _, particle in pairs(self.particles) do

		particle.lifetime = particle.lifetime + dT

		if particle.lifetime > particle.lifelength then
			table.remove(self.particles, idx)
			self.population =  table.Count(self.particles)
		else
			particle.position = particle.initpos + (particle.lifetime * particle.velocity) + (particle.lifetime * particle.lifetime * particle.acceleration)
		end
		idx = idx + 1
	end

end

function ENT:DrawParticles()

	if self:GetNWInt("system_state") == 1 then //is playing?
		
		local material = self:GetNWString("param_billboard")
		if material == nil or material == "" or material == "nil" then
			render.SetMaterial(TEMP)
		else
			render.SetMaterial(Material(material))
		end

		for _, particle in pairs(self.particles) do
		
			render.DrawSprite(self.Entity:GetPos() + particle.position, particle.radius, particle.radius, Color(255,255,255,255))
			
		end
	end

end

function ENT:Initialize()
	
	self.particles = {}
	self.population = 0

end


function ENT:Think()

	local populationLimit = self:GetNWInt("system_population")
	
	if self:GetNWInt("system_state") == 1 then //is playing?

		if self.population < populationLimit then
			self:CreateParticle()
		else
			while self.population > populationLimit do
				table.remove(self.particles, math.random(1,self.population))
				self.population =  table.Count(self.particles)
			end
		end

		self:UpdateParticles()

	elseif self.population > 0 then

		while self.population > 1 do
			table.remove(self.particles, math.random(1,self.population))
			self.population =  table.Count(self.particles)
		end
	end
	self:NextThink(CurTime() + 10)
end


function ENT:Draw()

	local isPlaying = self:GetNWInt("system_state") 
	local showOrigin = self:GetNWInt("system_show_origin")

	if LocalPlayer():GetNWInt("show_objects") == 1 then
		if isPlaying == 1 then	
			if showOrigin == 1  then
				render.SetMaterial(NODE_MATERIAL)
				render.DrawSprite(self.Entity:GetPos(), 32, 32, Color(255,255,255,255))
			end

		else
			render.SetMaterial(NODE_MATERIAL)
			render.DrawSprite(self.Entity:GetPos(), 32, 32, Color(255,255,255,255))
		end
	end
	self:DrawParticles()
	
end

function ENT:DrawTranslucent()
	
	local isPlaying = self:GetNWInt("system_state") 
	local showOrigin = self:GetNWInt("system_show_origin")

	if LocalPlayer():GetNWInt("show_objects") == 1 then
		if isPlaying == 1 then
			
			if showOrigin == 1 then
				render.SetMaterial(NODE_MATERIAL)
				render.DrawSprite(self.Entity:GetPos(), 32, 32, Color(255,255,255,255))
			end

		else
			render.SetMaterial(NODE_MATERIAL)
			render.DrawSprite(self.Entity:GetPos(), 32, 32, Color(255,255,255,255))
		end
	end
	self:DrawParticles()
end
